const Koa = require('koa');

const app = new Koa();

const static = require('koa-static');

const path = require('path');

app.use(static(path.join(process.cwd(),'public')));  ///api/list

// post 请求 传递的参数  bodyparser
//req,res,next

app.use(async (ctx,next) => {
    let startTime = new Date().getTime();
    // console.log(ctx.path);
    console.log("洋葱圈第一层开始")
    ctx.body = 'timer';    
    // console.log(ctx.response);
    await next();
    console.log("洋葱圈第一层结束")
    let endTime  = new Date().getTime();
    let timer = endTime - startTime;
    console.log('end')
})

app.use(async(ctx,next) => {
    console.log("洋葱圈第二层开始")
    await next()
    console.log("洋葱圈第二层结束")
})

function delay(){
    return new Promise((resolve,reject) => {
        setTimeout(() => {
            resolve('123')
        },2000)
    })
}

app.use(async (ctx,next) => {
    console.log("洋葱圈第三层开始")
    let num = await delay();
    await next()
    console.log("洋葱圈第三层结束")
})


app.listen(process.env.PORT || 3000,() => {
    console.log("服务启动成功")
})


// axios.get('/api/list').then(res => {
//     return  axiso.get('')
//     console.log(res)
// }).then(twoRes => {
//     return axiso.get('')
// }).then(threeRes => {

// })
// .catch(error => {
//     cos
// })


// ajax({
//     url:'',
//     success(res){
//         let id = 
//         ajax({
//             url:'',
//             success(twores){
//                 ajax()
//             }
//         })
//     }
// })